<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="使用方向" prop="expectUseOpt">
        <el-select v-model="queryParams.expectUseOpt" placeholder="请选择使用方向">
          <el-option v-for="dict in expectUseOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="small"  @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small"   @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-row>
    <el-table v-loading="loading" :data="productList">
      <el-table-column label="编号" prop="id" width="60" align="center"/>
      <el-table-column label="使用单位"  prop="collegeOrgName"  :show-overflow-tooltip="true"/>
      <el-table-column label="名称" prop="assetName" :show-overflow-tooltip="true" width="100"/>
      <el-table-column label="规格" prop="specs" width="80" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="项目经费" prop="fundsItem"  :show-overflow-tooltip="true" width="80"/>
      <el-table-column label="使用方向" prop="expectUseOpt" align="center" :formatter="userForFormat" width="80"/>
      <el-table-column label="购买日期" prop="purchaseDate" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.purchaseDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单价" align="center" prop="unitPrice" width="50"/>
      <el-table-column label="数量" align="center" prop="quantity" width="50"/>
      <el-table-column label="余量" align="center" prop="availableNum" width="50"/>
      <el-table-column label="操作" align="center" width="50">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUse(scope.row)"
            v-if="isPermit('labbase:product_use:query')">管理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
      <ProductUse ref="productUse" @refresh="getList" v-if="isPermit('labbase:product_use:query')"></ProductUse>
  </div>
</template>

<script>
  import {
   listProduct
  } from "@/api/labbase/product.js";
  import ProductUse from "@/views/labbase/product_use";
  export default {
    name: "Product",
    components: {ProductUse},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 子表选中数据
        expectUseOptOptions: [],
        // 总条数
        total: 0,
        // 备品表格数据
        productList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          collegeOrgId: null,
          expectUseOpt: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
      };
    },
    props: {
      orgId: Number,
    },
    watch: {
      orgId(val) {
        this.queryParams.collegeOrgId = val;
        this.getList();
      },
    },
    created() {
      this.getList();
      this.getDicts("expectUseOpt").then((response) => {
        this.expectUseOptOptions = response.data;
      });
    },
    methods: {
      /** 查询备品列表 */
      getList() {
        this.loading = true;
        listProduct(this.queryParams).then((response) => {
          this.productList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 流程状态字典翻译
      userForFormat(row, column) {
        return this.selectDictLabel(this.expectUseOptOptions, row.expectUseOpt);
      },
     /* orgFormat(row,column) {
        return this.$parent.$parent.$parent.$parent.$parent.getTreeNodeLabel(row.collegeOrgId);
      }, */
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 修改按钮操作 */
      handleUse(row) {
        this.$refs["productUse"].openUseEdit(row);
      },
      isPermit(permitKey){
        if(this.$store.state.user.name=='admin'){
          return true;
        }
        let  permissions = this.$store.getters.permissions;
        return permissions.includes(permitKey);
      },
    },
  };
</script>
